/**
 * User: Obi
 * Date: 02.08.11
 * Time: 23:21
 */
package com.jediflasher.game_client.data {
import com.jediflasher.game_client.events.model.LoginDataEvent;
import com.jediflasher.game_client.server.messages.LoginResponse;
import com.jediflasher.game_client.server.messages.LoginResponse.LoginResponseStatus;
import com.netease.protobuf.Message;

[Event(name="loginSuccess", type="com.jediflasher.game_client.events.model.LoginDataEvent")]
[Event(name="loginError", type="com.jediflasher.game_client.events.model.LoginDataEvent")]

public class LoginData extends BaseData {

	public function LoginData() {
		super(null);
	}

	public var status:int;

	public var id:int;

	override public function read(message:Message):void {
		var loginMessage:LoginResponse = message as LoginResponse;
		if(!loginMessage) throw TypeError("LoginData cand rea only LoginResponse");

		if(loginMessage.hasUserId) {
			this.id = loginMessage.userId;
		}

		if(loginMessage.hasStatus) {
			this.status = loginMessage.status;

			if(this.status == LoginResponseStatus.OK) {
				super.dispatchEvent(new LoginDataEvent(LoginDataEvent.LOGIN_SUCCESS));
			}
			else {
				super.dispatchEvent(new LoginDataEvent(LoginDataEvent.LOGIN_ERROR));
			}
		}
	}
}
}
